#!/usr/bin/env python
# coding=utf-8
#--------------------------------------------------------------------------
# 文件名:       Translator.py 
# 说明：        目的、作用说明
#
# 版本：        0.01
# 作者：        xiao
#
# 创建时间：    2011-11-19 
# 许可证：      Apache License V2.0
#--------------------------------------------------------------------------

import CollectBase as cb
from pyquery import PyQuery as pq
import os
import time
import random

## bing的翻译接口
#
class TranslatorCall(cb.CollectBase):

	## 方法注释
	#
	#  @param
	#  @return
	#
	def parseData(self, txt, resp, urlDict):

		if os.path.isfile("num_q"):
			num_q = int(open("num_q").read().strip()) + 1

			if num_q > random.randint(20, 30):
				time.sleep(random.randint(30, 80))

			open("num_q", "w").write(str(num_q))
		else:
			open("num_q", "w").write("1")

		d = pq(txt)
		for dt in d(".result-title a"):
			open("d.txt", "a").write("http://ezinearticles.com%s\n" % pq(dt).attr("href"))
		outDict = {"url": "http://ezinearticles.com%s" % d(".next").attr("href")}
		return outDict
	#------------------------------------------ End def parseData()
#------------------------------------------------------ End class Translator()

## 测试 -----------------------------------------
def outTest(txt):
	open("te.xml", "w").write(txt)

if __name__ == "__main__":
	TranslatorCall([{"url":"http://ezinearticles.com/search/?q=dvd"}])
